package com.yiyahui.project.erp.mq;

import com.alibaba.fastjson.JSON;
import com.yiyahui.project.erp.domain.YiyaClinicPerson;
import com.yiyahui.project.erp.domain.YiyaMarketReservation;
import com.yiyahui.project.erp.domain.eventEntity.PayMoneyEntity;
import com.yiyahui.project.erp.service.IYiyaClinicPersonService;
import com.yiyahui.project.erp.service.IYiyaMarketReservationService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

import static org.assertj.core.util.DateUtil.now;


@Component
@RocketMQMessageListener(topic = "${mq.erp.topic}",consumerGroup = "${mq.erp.consumer.group.name}",messageModel = MessageModel.BROADCASTING)
public class CancelMQListener implements RocketMQListener<MessageExt> {

    @Autowired
    private IYiyaMarketReservationService yiyaMarketReservationService;

    @Autowired
    private IYiyaClinicPersonService yiyaClinicPersonService;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            String keys = messageExt.getKeys();
            String tags = messageExt.getTags();
            String body = new String(messageExt.getBody(), "UTF-8");
            PayMoneyEntity payMoneyEntityEvent = JSON.parseObject(body, PayMoneyEntity.class);
            if (payMoneyEntityEvent != null) {
                System.out.println("接受到消息");
                BigDecimal afterOwingAmount = payMoneyEntityEvent.getAfterOwingAmount(); // 之后欠费
                BigDecimal afterPayAmount = payMoneyEntityEvent.getAfterPayAmount(); // 之后缴费
                YiyaClinicPerson yiyaClinicPersonResponse = yiyaClinicPersonService.selectYiyaClinicPersonById(payMoneyEntityEvent.getClinicPersonId());
                YiyaMarketReservation yiyaMarketReservationResponse = yiyaMarketReservationService.selectYiyaMarketReservationById(yiyaClinicPersonResponse.getMarketReservationId());
                YiyaMarketReservation yiyaMarketReservationRequest = new YiyaMarketReservation();
                YiyaClinicPerson yiyaClinicPersonRequest = new YiyaClinicPerson();
                yiyaClinicPersonRequest.setId(yiyaClinicPersonResponse.getId());
                if(payMoneyEntityEvent.getMainClosingEntriesType() == 1){
                    //首开单，以事后状态为准
                    yiyaClinicPersonRequest.setOverMoney(yiyaClinicPersonResponse.getOverMoney().add(afterPayAmount));
                    yiyaClinicPersonRequest.setArrearage(yiyaClinicPersonResponse.getArrearage().add(afterOwingAmount));
                }else{
                    yiyaClinicPersonRequest.setOverMoney(yiyaClinicPersonResponse.getOverMoney().add(afterPayAmount).subtract(payMoneyEntityEvent.getBeforePayAmount()));
                    yiyaClinicPersonRequest.setArrearage(yiyaClinicPersonResponse.getArrearage().add(afterOwingAmount).subtract(payMoneyEntityEvent.getBeforeOwingAmount()));
                }
                if (!"已开单".equals(yiyaClinicPersonResponse.getBillingState())) {
                    yiyaClinicPersonRequest.setBillingState("已开单");
                }
                if (yiyaMarketReservationResponse != null && !"已开单".equals(yiyaMarketReservationResponse.getBillingState())) {
                    yiyaMarketReservationRequest.setId(yiyaMarketReservationResponse.getId());
                    yiyaMarketReservationRequest.setBillingState("已开单");
                    yiyaMarketReservationRequest.setTurnoverMoney(payMoneyEntityEvent.getTurnoverMoney()); // 成交金额为 首单应收金额
                    yiyaMarketReservationRequest.setCheckoutTime(now());
                    yiyaMarketReservationService.updateYiyaMarketReservation(yiyaMarketReservationRequest);
                }
                yiyaClinicPersonService.updateYiyaClinicPerson(yiyaClinicPersonRequest);
            }
            System.out.println(keys);
            System.out.println(body);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("消费失败");
        }

    }
}
